/*
 * TodayClock.fx
 *          VisualClock Project
 *   This app draws a visual clock showing the current time counting up
 *   See other files for different clock types
 *
 * Created on Nov 15, 2009, 9:43:13 AM
 *
 * v0.1a 111509 - initial creation (Scott Szretter)
 *
 *
 */

package visualclock;

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.animation.*;
import java.util.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.util.Math;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.control.Label;
import java.text.SimpleDateFormat;

var hours;
var minutes;
var seconds;
var date;
var day;
var month;
var year;
var cal;

function createCalendar() {
    var date = new Date();
    def calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar
}

var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var today:String = "{sdf.format(Calendar.getInstance().getTime())}";

var time = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 100ms
            action:function() {
                cal = createCalendar();
                seconds = (cal.get(Calendar.SECOND) + (cal.get(Calendar.MILLISECOND) as Float)/1000)/60;
                minutes = (cal.get(Calendar.MINUTE) + seconds)/60;
                hours = (cal.get(Calendar.HOUR) + minutes)/12;
                today = "{sdf.format(Calendar.getInstance().getTime())}";
         }}]}





class ThreeLineClock extends CustomNode { //define our clock (three line style)
    public var cX=10;
    public var cY=10;
    public var strokeWidth = 5;
    public var radius=50;
    public var secLength= 180.0;
    public var minLength= 90.0;
    public var hourLength= 30.0;
    public var todayText = "";
    override function create() {
        return Group {
            opacity: 0.85
            content:[
                Arc {// second
                    centerX: cX, centerY: cY
                    radiusX: radius, radiusY: radius
                    startAngle: 90, length:bind -secLength+strokeWidth*360.0/(4*Math.PI*radius)
                    stroke: Color.GREEN
                    fill: null
                    strokeWidth: strokeWidth
                    }
                Arc {// minute
                    centerX: cX, centerY: cY
                    radiusX: radius-17, radiusY: radius -17
                    startAngle: 90, length:bind -minLength+strokeWidth*360.0/(4*Math.PI*radius)
                    stroke: Color.RED
                    fill: null
                    strokeWidth: strokeWidth
                    }
                Arc {// hour
                    centerX: cX, centerY: cY
                    radiusX: radius-34, radiusY: radius-34
                    startAngle: 90, length:bind -hourLength+strokeWidth*360.0/(4*Math.PI*radius)
                    stroke: Color.BLUE
                    fill: null
                    strokeWidth: strokeWidth
                    }
                Label {
                    translateY : cY+radius + 10
                    translateX : cX - radius/2;
                    text: bind todayText
                    textFill: Color.GOLD
                    }
            ]; //content
        }; //return group
    } // function create
} // class

public function run () {
    time.play();
//println("{today}");
    var scene:Scene = Scene {
        width: 300
        height: 340
        fill: Color.BLACK
        content: [
            //ImageView {   image: Image {
            //url: "{__DIR__}backgroundImage.png"
            //}}
            ThreeLineClock {
                radius:120
                cX: 150
                cY: 160
                strokeWidth:15
                secLength:bind 360*seconds
                minLength: bind 360*minutes
                hourLength:bind 360*hours
                todayText: bind "today: {today}"
            }
        ] //content
    } //scene

    def stage:Stage = Stage {
        title: "VisualClock"
        scene: scene
    } //stage

    stage.visible = true;
} //function run


